<!--图片上传控件，改造自mpvue-weui-->
<template>
  <div class="weui-uploader" :class="topbg === 'topbg' ? 'topbg':''">
    <div class="weui-uploader__hd" v-if="showTip">
      <div class="weui-uploader__title">图片上传</div>
      <div class="weui-uploader__info">{{files.length}}/{{maxLength}}</div>
    </div>
    <div class="weui-uploader__bd">
      <div v-if="showType" class="weui-uploader__files" id="uploaderFiles">
        <!-- {{ files }} -->
        <div v-for="(item ,index) in files" :key="index" :class="(bennerLen + files.length) >= 3 ? 'mar-s-13 ' : 'mar-s-23'">
          <div class="weui-uploader__file"
            :class="uploadDefault && (bennerLen + files.length) > 3 && bennerLen > 3 ? 'mar-s-11' : 'mar-s-12 u-list-' + bennerLen + '-' + index">
            <image class="weui-uploader__img" :src="item" mode="aspectFill" @click="previewImage" :id="item" />
            <div class="delete-icon" @click="deleteImg" :id="item" :data-index="index" :key="index"></div>
          </div>
        </div>
      </div>
      <div :class="(bennerLen + files.length) >= 3 ? 'mar-s-13' : 'mar-s-23'">
        <div :class="uploadDefault && (bennerLen + files.length) > 3 ? 'mar-s-1' : 'mar-s-2'">
          <div v-if="topbg === 'topbg' && (bennerLen + files.length) < 5 && bennerLen === 2"
            :class="(bennerLen + files.length) === 3 || (bennerLen + files.length) === 6 ? 'mar-l-0 ' + 'files-' + count:'mar-l-15 ' + 'files-' + count"
            class="weui-uploader__input-box">
            <div class="weui-uploader__input" @click="chooseImage"></div>
          </div>
          <div v-if="topbg === 'topbg' && (bennerLen + files.length) < 5 && bennerLen !== 2"
            :class="(bennerLen + files.length) === 3 || (bennerLen + files.length) === 6 ? 'mar-l-0':'mar-l-15 '"
            class="weui-uploader__input-box">
            <div class="weui-uploader__input" @click="chooseImage"></div>
          </div>
        </div>
      </div>
      <div v-if="topbg !== 'topbg' && (files.length < maxLength)" class="weui-uploader__input-box">
        <div class="weui-uploader__input" @click="chooseImage"></div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        isComplete: true,
        files: []
      }
    },
    onLoad() {
      // 初始化data
      Object.assign(this.$data, this.$options.data())
    },
    props: {
      uploadFiles: {
        type: Array,
        default: []
      },
      maxLength: {
        type: Number,
        default: 20
      },
      showTip: {
        type: Boolean,
        default: true
      },
      showType: {
        type: Boolean,
        default: true
      },
      topbg: {
        type: String,
        default: ''
      },
      count: {
        type: Number,
        default: 0
      },
      bennerLen: {
        type: Number,
        default: 0
      },
      uploadDefault: {
        type: Boolean,
        default: true
      }
    },
    watch: {
      count(val) {
        // console.log('count')
        // console.log(val)
        // if (val === 0) {
        //   this.files = []
        // } else {
        //   this.files = this.files.concat(this.uploadFiles)
        // }
      }
    },
    onShow() {
      this.$pt.removeStorageSync('chooseLocation')
    },
    mounted() {
      this.files = this.files.concat(this.uploadFiles)
    },
    methods: {
      chooseImage(e) {
        this.showType = true
        let that = this
        that.$pt.chooseImage({
          count: that.maxLength, // 默认5
          sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有
          sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
          success: function(res) {
            // console.log(that.count)
            that.files = that.files.concat(res.tempFilePaths)
            res.files = that.files
            that.$emit('upLoadSuccess', res)
          },
          fail: function(res) {
            that.$emit('upLoadFail', res)
            // 修改提交记录
          },
          complete: function(res) {
            that.$emit('upLoadComplete', res)
          }
        })
      },
      previewImage(e) {
        this.$pt.setStorageSync('chooseLocation', true)
        this.$pt.previewImage({
          current: e.currentTarget.id, // 当前显示图片的http链接
          urls: this.files // 需要预览的图片http链接列表
        })
      },
      deleteImg(e) {
        Array.prototype.indexOf = function(val) { // eslint-disable-line
          for (let i = 0; i < this.length; i++) {
            if (this[i] === val) return i
          }
          return -1
        }
        Array.prototype.remove = function(val) { // eslint-disable-line
          let index = this.indexOf(val)
          if (index > -1) {
            this.splice(index, 1)
          }
        }
        this.files.remove(e.currentTarget.id)
        let uploadDeleteObj = {
          src: e.currentTarget.id,
          index: e.currentTarget.dataset.index,
          files: this.files
        }
        this.$emit('uploadDelete', uploadDeleteObj)
      }
    }
  }
</script>

<style lang="less" scoped>
  .mar-s-11 {
    margin-top: 10px;
  }
  .mar-s-12 {
    margin-top: 0px;
    &.u-list-2{
      margin-top: 0px!important;
    }
  }
  .mar-s-1 .weui-uploader__input-box{
    margin-top: 10px;
  }
  .mar-s-2 .weui-uploader__input-box{
    margin-top: 0px;
  }
  .mar-s-13 .weui-uploader__input-box{
    margin-top: 10px!important;
  }
  .mar-s-23 .weui-uploader__input-box{
    margin-top: 0px!important;
  }
  .mar-s-23 .mar-s-11{
    margin-top: 0px!important;
  }
  .mar-s-13 .mar-s-12{
    margin-top: 10px!important;
    &.u-list-1-0{
      margin-top: 0px!important;
    }
    &.u-list-1-1{
      margin-top: 0px!important;
    }
    &.u-list-2-0{
      margin-top: 0px!important;
    }
  }
  .mar-s-13 .mar-s-2{
    .files-0{
      margin-top: 0px!important;
    }
  }
  .topbg {
    .weui-uploader__img{
      width: 105px;
      height: 80px;
    }
    .delete-icon{
      right: -10px;
    }
  }
  .weui-uploader__file {
    position: relative;
  }
  .weui-uploader__bd {
    overflow: visible;
  }
  .delete-icon {
    position: absolute;
    width: 20px;
    height: 20px;
    background: #f43530;
    right: 0;
    top: -10px;
    border-radius: 20px;
    z-index: 35;
  }
  .delete-icon::before {
    content: '';
    width: 13px;
    height: 2px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
  }
</style>
